home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_300 / 376_04 / os2tool.003 / WORDS.C < prev    next >
Encoding:
C/C++ Source or Header  |  1992-08-28  |  2.7 KB  |  127 lines

  1. /*
  2. * WORDS.C - Counts the occurance of characters, words and lines in a given text file.
  3. *
  4. * PROGRAMMER:        Martti Ylikoski
  5. * CREATED:        2.12.1990
  6. */
  7. static char *VERSION = "Version 1.1. Copyright (c) Martti Ylikoski. 1990, 1991. " ;
  8. /*
  9. */
  10.  
  11. #include <stdio.h>
  12. #include <malloc.h>
  13. #include <stdlib.h>
  14. #include <string.h>
  15. #include <ctype.h>
  16. #include <param.h>
  17. #include <paramstd.h>
  18. #define INCL_DOS
  19. #include <os2.h>
  20.  
  21.  
  22. static char *progname ;
  23. extern unsigned long pflags ;
  24.  
  25. ParamEntry pentry[12] = {
  26.      "P", &ParamSetPause, 0,
  27.      "F", &ParamSetFold, 0,
  28.      "V", &ParamSetVerbose, 0,
  29.      "R", &ParamSetReport, 0,
  30.      "S", &ParamSetSubDirs, 0,
  31.      "?", &ParamSetHelp, 1,
  32.      "H", &ParamSetHelp, 1,
  33.      "NOD", &ParamSetNoDefault, 0,
  34.      "TEST", &ParamSetTest, 0,
  35.      "Y", &ParamSetYes, 0,
  36.      "N", &ParamSetTest, 0,
  37.      "\0", NULL, 0
  38. } ;
  39.  
  40. ParamBlock params = {
  41.     "/-",   IGNORECASE | NOPRIORITY | NOTRIGGERSALLOWED ,
  42.     pentry
  43. } ;
  44.  
  45. /* local prototypes */
  46. static int words( char *fname) ;
  47.  
  48. int main(int argc, char *argv[])
  49. {
  50. FILE *fptr ;
  51. int i, c ;
  52.  
  53.    progname = argv[0] ;
  54.  
  55.    ParamHandle(¶ms, &argc, argv) ;
  56.  
  57.    if (pflags & PA_HELP)
  58.    {
  59.       printf("%s - count the occurance of characters, words and lines in a given text file.", progname) ;
  60.       puts(VERSION) ;
  61.       puts("Usage: WORDS file(s) [/H | /?]") ;
  62.       return( 1 ) ;
  63.    }
  64.  
  65.    if (argc == 1)
  66.       words("-") ;
  67.    else
  68.       for ( i = 1 ; i < argc ; i++)
  69.      words(argv[i]) ;
  70.  
  71.    return( 0 ) ;
  72. }
  73.  
  74. static int words( char *fname)
  75. {
  76. long characters, words, lines ;
  77. int in_word, in_line ;
  78. FILE *fptr ;
  79. int i, c ;
  80.  
  81.    characters = 0L ; words = 0L ; lines = 0L ;
  82.    in_word=FALSE ;
  83.    in_line = FALSE ;
  84.    printf("[%s]\n",fname) ;
  85.  
  86.    if (strcmp(fname, "-") == 0)
  87.       fptr = stdin ;
  88.    else
  89.       /* open file */
  90.       if ((fptr = fopen(fname, "r")) == NULL)
  91.       {
  92.      printf("%s: unable to open file %s ...\n", progname, fname) ;
  93.      printf("Bypassing file...\n") ;
  94.      return( 1 ) ;
  95.       }
  96.  
  97.    /* read from file */
  98.    while ( (c = fgetc(fptr)) != EOF)
  99.    {
  100.       characters ++ ;
  101.       in_line = TRUE ;
  102.       if (isspace(c) != 0 && in_word == TRUE)
  103.      words ++ ;
  104.       else
  105.      if (isspace(c) == 0) /* not a space character */
  106.         in_word = TRUE ;
  107.  
  108.       if (c == '\n')
  109.       {
  110.      in_word = FALSE ;
  111.      in_line = FALSE ;
  112.      lines ++ ;
  113.       }
  114.    }
  115.  
  116.    /* close file */
  117.    fclose (fptr ) ;
  118.  
  119.    if (in_line == TRUE)
  120.       lines ++ ; /* last line not yet counted */
  121.    if (in_word == TRUE)
  122.       words ++ ; /* last word not yet counted */
  123.    printf("characters = %d\nwords = %d\nlines = %d\n\n", characters, words, lines) ;
  124.    return( 0 ) ;
  125.  
  126. }
  127.